package com.gxy.learn.pay.controller;

import com.gxy.learn.pay.common.Result;
import com.gxy.learn.pay.controller.param.AliPayOrderReqParam;
import com.gxy.learn.pay.service.PayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author gxy
 */
@Validated
@Slf4j
@RestController
@RequestMapping("/alipay/api")
public class AlipayController {

    @Autowired
    private PayService service;

    /**
     * ios下单
     *
     * @param commodityIds
     * @return
     */
    @GetMapping(value = "/order")
    public Result order(String commodityIds) {
        return service.aliPayCreateOrder(commodityIds);
    }

    /**
     * 支付宝H5下单
     *
     * @param reqParam
     * @return
     */
    @PostMapping(value = "/h5Order")
    public Result h5Order(@RequestBody AliPayOrderReqParam reqParam) {
        return service.aliPayH5CreateOrder(reqParam);
    }

    /**
     * 支付宝回调 回写订单支付状态
     * 注意 回调接口需要再 whitelist.yml配置中添加配置 要不然会被拦截
     *
     * @param request
     * @return
     */
    @PostMapping(value = "/notify")
    public void notify(HttpServletRequest request) {
        service.aliPayNotify(request);
    }

    /**
     * app回调 返回订单支付状态
     *
     * @return
     */
    @GetMapping(value = "/app/notify")
    public Result appNotify(String outTradeNo) {
        return service.aliPayAppNotify(outTradeNo);
    }
}
